package com.apestech.framework.xml.sql.modul;

import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.json.JSONOrder;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.xml.plugin.modul.GridHead;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2018-12-06-17:29
 */
public class QueryhTw extends StringUtil{

    public class Pivot{
        private String field;
        private String express;
        private String alais;
        public Pivot(JSONObject json){
            this.field=json.getString("field");;
            this.express=json.getString("express");;
        }

        public String getField() {
            return field;
        }

        public String getExpress() {
            return express;
        }

        public String getAlais() {
            return alais;
        }

        public void setAlais(String alais) {
            this.alais = alais;
        }
    }

    private String pivotField;
    private String dataField;
    private String table;
    private String idField;
    private String nameField;
    private GridHead gridHead;
    private JSONOrder pivotlist;
    private JSONArray dataFieldHead;

    public void addBase(JSONObject json){
        this.dataField=json.getString("dataField");
        if(this.dataField!=null) this.dataField=this.dataField.toUpperCase();
        this.table=json.getString("table");
        if(this.table!=null) this.table=this.table.toUpperCase();
        this.idField=json.getString("idField");
        if(this.idField!=null) this.idField=this.idField.toUpperCase();
        this.nameField=json.getString("nameField");
        if(this.nameField!=null) this.nameField=this.nameField.toUpperCase();
        this.pivotField="";
    }

    public String getDataField() {
        return dataField;
    }

    public String getTable() {
        return table;
    }

    public String getIdField() {
        return idField;
    }

    public String getNameField() {
        return nameField;
    }

    public void addPivotHead(SqlResultSet rs) throws Exception {
        gridHead.addPivot(dataFieldHead);
        pivotField=","+pivotField+",";
        gridHead.addPivotField(rs, this);
    }

    public void addPivot(JSONObject json){
        if(pivotlist==null) pivotlist=new JSONOrder();
        Pivot p=new Pivot(json);
        pivotlist.put(p.getField(), p);
        if(!"".equals(pivotField)) pivotField=pivotField+",";
        pivotField=pivotField+json.getString("field");
    }

    public void setPivotAlais(String field, String alais){
        if(pivotlist==null) return;
        if(pivotlist.get(field)==null) return;
        Pivot p=(Pivot) pivotlist.get(field);
        p.setAlais(alais);
        pivotlist.put(p.getField(), p);
    }

    public boolean isPivotField(String field){
        return pivotField.indexOf(","+field+",")>=0;
    }

    public JSONArray getHead() {
        return gridHead.getHead();
    }

    public String getPivotOldField(String field) {
        String key= gridHead.getPivotmap().getString(field);
        if(key==null) key=field;
        return key;
    }

    public void setDataFieldHead(JSONArray dataFieldHead) throws Exception {
        this.dataFieldHead=dataFieldHead;
    }

    public JSONOrder getPivotlist() {
        return pivotlist;
    }

    public ArrayList<GridHead.PivotHead> getHeadlist(){
        return gridHead.getPivotlist();
    }

    public void setGridHead(GridHead gridHead) {
        this.gridHead = gridHead;
    }

    public GridHead getGridHead() {
        return gridHead;
    }
}
